Option Explicit

Private Const TEST_TABLE_NAME As String = "table1"
Private Const TEST_POINT As String = "."
Private Const TEST_COL_NAME As String = "col1"
Private Const TEST_SHARE_LINE_COMMENT As String = "--aaaaaa"
Private Const TEST_SINGLE_LINE_COMMENT As String = "--bbbbb"

' ======================================================================
' 执行所有测试用例
' ======================================================================
Public Sub test_all_case()
    ' ==================================================
    ' Public 方法测试
    ' ==================================================
    ' 测试: Init 方法
    test_ThisClass_Init

    ' ==================================================
    ' ISqlFormatterElem 接口实现测试
    ' ==================================================
    ' 测试: addComponent 方法
    test_ISqlFormatterElem_addComponent
    ' 测试: addComponentToFirst 方法
    test_ISqlFormatterElem_addComponentToFirst
    ' 测试: canAddComponentToFirst 方法
    test_ISqlFormatterElem_canAddComponentToFirst
    ' 测试: getAlignmentElemGroups 方法
    test_ISqlFormatterElem_getAlignmentElemGroups
    ' 测试: setElemNestCount 方法
    test_ISqlFormatterElem_setElemNestCount
    ' 测试: setPrefixSpaceCount 方法
    test_ISqlFormatterElem_setPrefixSpaceCount
    ' 测试: hasBoundaryParenthesis 方法
    test_ISqlFormatterElem_hasBoundaryParenthesis
    ' 测试: addBoundaryLeftParenthesis 方法
    test_ISqlFormatterElem_addBoundaryLeftParenthesis
    ' 测试: addBoundaryRightParenthesis 方法
    test_ISqlFormatterElem_addBoundaryRightParenthesis
    ' 测试: getType 方法
    test_ISqlFormatterElem_getType
    ' 测试: isComment 方法
    test_ISqlFormatterElem_isComment
    ' 测试: isShareLineComment 方法
    test_ISqlFormatterElem_isShareLineComment
    ' 测试: isSingleLineComment 方法
    test_ISqlFormatterElem_isSingleLineComment
    ' 测试: isEndsWithComment 方法
    test_ISqlFormatterElem_isEndsWithComment
    ' 测试: isMultiLine 方法
    test_ISqlFormatterElem_isMultiLine
    ' 测试: toString + toStringArray 方法
    test_ISqlFormatterElem_toString_toStringArray
End Sub


' ======================================================================
' 测试通用处理
' ======================================================================

' 获取逻辑运算符: and
Private Function getAndToken() As SqlToken
    Set getAndToken = New SqlToken
    getAndToken.Init "and", SqlTokenType.KEY_AND
End Function

' 获取一个条件运算符: <=
Private Function getLeConditionOperator() As SqlToken
    Set getLeConditionOperator = New SqlToken
    getLeConditionOperator.Init "<=", SqlTokenType.OPERA_LE
End Function

' 获取一个任意名称的 STR_NAME token
Private Function getStrNameToken(ByRef s As String) As SqlToken
    Set getStrNameToken = New SqlToken
    getStrNameToken.Init s, SqlTokenType.STR_NAME
End Function

' 获取表名 token
Private Function getTableNameToken() As SqlToken
    Set getTableNameToken = New SqlToken
    getTableNameToken.Init TEST_TABLE_NAME, SqlTokenType.STR_NAME
End Function

' 获取 [.] token
Private Function getPointToken() As SqlToken
    Set getPointToken = New SqlToken
    getPointToken.Init TEST_POINT, SqlTokenType.POINT
End Function

' 获取列名 token
Private Function getColNameToken() As SqlToken
    Set getColNameToken = New SqlToken
    getColNameToken.Init TEST_COL_NAME, SqlTokenType.STR_NAME
End Function

' 获取和前一个元素在同一行的注释
Private Function getShareLineCommentToken() As SqlToken
    Set getShareLineCommentToken = New SqlToken
    getShareLineCommentToken.Init TEST_SHARE_LINE_COMMENT, SqlTokenType.SHARE_LINE_COMMENT
End Function

' 获取一个单行的注释
Private Function getSingleLineCommentToken() As SqlToken
    Set getSingleLineCommentToken = New SqlToken
    getSingleLineCommentToken.Init TEST_SINGLE_LINE_COMMENT, SqlTokenType.SINGLE_LINE_COMMENT
End Function

' 获取一个逗号的元素
Private Function getCommaToken() As SqlToken
    Set getCommaToken = New SqlToken
    getCommaToken.Init ",", SqlTokenType.COMMA
End Function

' 获取一个没有边界括号的多行元素
' 多行连接的结果:
' col1 & ","
' col2
Private Function getMultiLineElemNoParenthesis() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")

    ' 2. 手动设置为多行元素，仍然是单行
    g.setNeedMultiLineWhenHasComma True

    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    Set getMultiLineElemNoParenthesis = elem
End Function

' 获取一个没有边界括号的多行元素
' 多行连接的结果:
' col1 & ","
' col2 --aaaaaa
Private Function getMultiLineElemNoParenthesisEndWithComment() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")

    ' 2. 手动设置为多行元素，仍然是单行
    g.setNeedMultiLineWhenHasComma True

    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    ' 5. 增加一个和前一个元素同一行的注释
    elem.addComponent getShareLineCommentToken

    Set getMultiLineElemNoParenthesisEndWithComment = elem
End Function

' 获取一个单行的函数体
' 连接的结果:
' (col1, col2)
Private Function getSingleLineFuncBody() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")


    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    ' 5. 设置边界括号
    elem.addBoundaryLeftParenthesis
    elem.addBoundaryRightParenthesis

    Set getSingleLineFuncBody = elem
End Function

' 获取一个多行的函数体
' 连接的结果:
' (
'     col1,
'     col2
' )
Private Function getMultiLineFuncBody() As ISqlFormatterElem
    Dim g As SqlFormatterCommaDeliMultiElem
    Set g = New SqlFormatterCommaDeliMultiElem

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' 1. 增加一个单行元素
    elem.addComponent getStrNameToken("col1")

    ' 2. 手动设置为多行元素，仍然是单行
    g.setNeedMultiLineWhenHasComma True

    ' 3. 在末尾添加一个逗号元素，仍然是单行的
    elem.addComponent getCommaToken

    ' 4. 再增加一个单行元素，变成多行的
    elem.addComponent getStrNameToken("col2")

    ' 5. 设置边界括号
    elem.addBoundaryLeftParenthesis
    elem.addBoundaryRightParenthesis

    Set getMultiLineFuncBody = elem
End Function


' ======================================================================
' Public 方法测试
' ======================================================================
' 测试: Init 方法
Private Sub test_ThisClass_Init()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol

    g.Init getStrNameToken("max")

    ' ==============================
    ' 2. 测试函数名
    ' ==============================
    Debug.Assert g.ISqlFormatterElem_toString(config) = "max"

End Sub

' ======================================================================
' ISqlFormatterElem 接口实现测试
' ======================================================================
' 测试: addComponent 方法
Private Sub test_ISqlFormatterElem_addComponent()
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim g As SqlFormatterFuncCol

    ' ==============================
    ' 1. 添加一个单行元素，然后再添加一个单行元素
    ' ==============================
    ' 1. 添加第一个单行元素
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getStrNameToken("c1")

    Debug.Assert g.ISqlFormatterElem_toString(config) = "max c1"

    ' 检查: 是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 2. 添加第二个元素
    g.ISqlFormatterElem_addComponent getStrNameToken("c2")
    Debug.Assert g.ISqlFormatterElem_toString(config) = "max c1 c2"

    ' 检查是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 3. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken

    ' 检查是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True


    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max c1 c2 --aaaaaa"

    ' ==============================
    ' 2. 添加一个多行元素，然后再添加一个单行元素
    ' ==============================
    ' 1. 添加第一个多行元素
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getMultiLineElemNoParenthesis

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "max col1,"
    Debug.Assert strArr.Item(1) = "col2"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False


    ' 2. 添加第二个单行元素
    g.ISqlFormatterElem_addComponent getStrNameToken("c2")
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "max col1,"
    Debug.Assert strArr.Item(1) = "col2 c2"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 3. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "max col1,"
    Debug.Assert strArr.Item(1) = "col2 c2 --aaaaaa"

    ' ==============================
    ' 3. 添加一个单行的函数体
    ' ==============================
    ' 1. 添加一个单行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getSingleLineFuncBody

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max(col1, col2)"

    ' 检查是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 2. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    ' 检查是单行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = False
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max(col1, col2) --aaaaaa"

    ' 3. 增加左边界括号，变成多行的
    g.ISqlFormatterElem_addBoundaryLeftParenthesis
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 2
    Debug.Assert strArr.Item(0) = "("
    Debug.Assert strArr.Item(1) = "    max(col1, col2) --aaaaaa"

    ' 4. 增加右边界括号，变成多行的
    g.ISqlFormatterElem_addBoundaryRightParenthesis
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "("
    Debug.Assert strArr.Item(1) = "    max(col1, col2) --aaaaaa"
    Debug.Assert strArr.Item(2) = ")"

    ' ==============================
    ' 4. 添加一个多行的函数体
    ' ==============================
    ' 1. 添加一个多行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getMultiLineFuncBody

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 4
    Debug.Assert strArr.Item(0) = "max("
    Debug.Assert strArr.Item(1) = "    col1,"
    Debug.Assert strArr.Item(2) = "    col2"
    Debug.Assert strArr.Item(3) = ")"

    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 不是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = False

    ' 2. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    ' 检查是多行的
    Debug.Assert g.ISqlFormatterElem_isMultiLine = True
    ' 检查: 是注释结尾
    Debug.Assert g.ISqlFormatterElem_isEndsWithComment = True

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 4
    Debug.Assert strArr.Item(0) = "max("
    Debug.Assert strArr.Item(1) = "    col1,"
    Debug.Assert strArr.Item(2) = "    col2"
    Debug.Assert strArr.Item(3) = ") --aaaaaa"
End Sub

' 测试: addComponentToFirst 方法
Private Sub test_ISqlFormatterElem_addComponentToFirst()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 测试: 无法增加元素
    ' ==============================
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    ' 1. 增加一个单行元素
    g.ISqlFormatterElem_addComponentToFirst getStrNameToken("x1")

    Debug.Assert g.ISqlFormatterElem_toString(config) = "max"
End Sub

' 测试: canAddComponentToFirst 方法
Private Sub test_ISqlFormatterElem_canAddComponentToFirst()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    ' ==============================
    ' 2. 测试: 默认无法添加
    ' ==============================

    Debug.Assert g.ISqlFormatterElem_canAddComponentToFirst = False
End Sub

' 测试: getAlignmentElemGroups 方法
Private Sub test_ISqlFormatterElem_getAlignmentElemGroups()
    Dim g As SqlFormatterFuncCol

    Dim groups As IObjArrayRef
    Dim group As IObjArrayRef

    ' ==============================
    ' 1. 默认没有条件组
    ' ==============================
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Debug.Assert g.ISqlFormatterElem_getAlignmentElemGroups Is Nothing

    ' ==============================
    ' 2. 添加一个单行元素: 没有条件组
    ' ==============================
    g.ISqlFormatterElem_addComponent getStrNameToken("c2")
    Debug.Assert g.ISqlFormatterElem_getAlignmentElemGroups Is Nothing

    ' ==============================
    ' 3. 添加一个包含条件的函数体
    ' ==============================
    ' 1. 添加一个包含条件的函数体
    Dim funcBody As SqlFormatterCommaDeliMultiElem
    Set funcBody = New SqlFormatterCommaDeliMultiElem

    Dim condition As SqlFormatterTwoElemCondition
    Set condition = New SqlFormatterTwoElemCondition
    funcBody.ISqlFormatterElem_addComponent condition

    Dim condition2 As SqlFormatterTwoElemCondition
    Set condition2 = New SqlFormatterTwoElemCondition
    funcBody.ISqlFormatterElem_addComponent condition2

    g.ISqlFormatterElem_addComponent funcBody

    Set groups = g.ISqlFormatterElem_getAlignmentElemGroups
    Debug.Assert groups.Count = 2
    ' 检查第一个条件
    Set group = groups.Item(0)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition
    ' 检查第二个条件
    Set group = groups.Item(1)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition2

    ' 2. 再向函数体中添加一个包含多个条件对象的组合对象
    ' 创建一个包含多个条件的组合对象
    Dim multiConditions As SqlFormatterCommaDeliMultiElem
    Set multiConditions = New SqlFormatterCommaDeliMultiElem
    Dim condition3 As SqlFormatterTwoElemCondition
    Set condition3 = New SqlFormatterTwoElemCondition
    Dim condition4 As SqlFormatterTwoElemCondition
    Set condition4 = New SqlFormatterTwoElemCondition
    multiConditions.ISqlFormatterElem_addComponent condition3
    multiConditions.ISqlFormatterElem_addComponent condition4

    ' 将这个组合对象保存到函数体
    funcBody.ISqlFormatterElem_addComponent multiConditions

    Set groups = g.ISqlFormatterElem_getAlignmentElemGroups
    Debug.Assert groups.Count = 4
    ' 检查第一个条件
    Set group = groups.Item(0)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition
    ' 检查第二个条件
    Set group = groups.Item(1)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition2
    ' 检查第三个条件
    Set group = groups.Item(2)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition3
    ' 检查第四个条件
    Set group = groups.Item(3)
    Debug.Assert group.Count = 1
    Debug.Assert group.Item(0) Is condition4

End Sub

' 测试: setElemNestCount 方法
Private Sub test_ISqlFormatterElem_setElemNestCount()
    ' 这里使用一个条件对象作为函数体，并通过对齐长度来检查

    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim condition As SqlFormatterTwoElemCondition
    ' ==============================
    ' 2. 测试: 增加一个条件元素
    ' ==============================
    ' 1. 创建一个条件对象
    Set condition = New SqlFormatterTwoElemCondition
    ' 设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    ' 设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    ' 设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    ' 设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")

    ' 2. 添加这个条件对象
    g.ISqlFormatterElem_addComponent condition

    ' 3. 设置嵌套深度
    g.ISqlFormatterElem_setElemNestCount 2, config

    ' 4. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 17 ' 8 + 9

End Sub

' 测试: setPrefixSpaceCount 方法
Private Sub test_ISqlFormatterElem_setPrefixSpaceCount()

    ' 这里使用一个条件对象作为函数体，并通过对齐长度来检查

    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim condition As SqlFormatterTwoElemCondition

    ' ==============================
    ' 2. 测试: 增加一个条件元素
    ' ==============================
    ' 1. 创建一个条件对象
    Set condition = New SqlFormatterTwoElemCondition
    ' 设置 and
    condition.ISqlFormatterCondition_setLogicOperator getAndToken
    ' 设置第一个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x1")
    ' 设置条件运算符 <=
    condition.ISqlFormatterCondition_setConditionOperator getLeConditionOperator
    ' 设置第二个元素
    condition.ISqlFormatterElem_addComponent getStrNameToken("x2")

    ' 2. 添加这个条件对象
    g.ISqlFormatterElem_addComponent condition

    ' 3. 设置嵌套深度
    g.ISqlFormatterElem_setPrefixSpaceCount 10, config

    ' 4. 检查条件对象的对齐长度
    ' and x1 <= x2
    ' 123456789
    Debug.Assert condition.ISqlFormatterAlignmentElem_getAlignmentSize(config) = 19 ' 10 + 9

End Sub

' 测试: hasBoundaryParenthesis 方法
Private Sub test_ISqlFormatterElem_hasBoundaryParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 测试
    ' ==============================
    ' 2. 增加左边界括号
    elem.addBoundaryLeftParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True

    ' 3. 增加右边界括号
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True

    ' 4. 重新创建对象，并只添加右边界括号
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    Set elem = g

    elem.addComponent getTableNameToken
    Debug.Assert elem.hasBoundaryParenthesis = False

    '    增加右边界括号
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.hasBoundaryParenthesis = True
End Sub

' 测试: addBoundaryLeftParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryLeftParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = "max " & TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis
    Debug.Assert elem.toString(config) = "(max " & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "(max " & TEST_TABLE_NAME

    ' ==============================
    ' 4. 添加多个左括号
    ' ==============================
    elem.addBoundaryLeftParenthesis(3)
    Debug.Assert elem.toString(config) = "((((max " & TEST_TABLE_NAME

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((((max " & TEST_TABLE_NAME
End Sub

' 测试: addBoundaryRightParenthesis 方法
Private Sub test_ISqlFormatterElem_addBoundaryRightParenthesis()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g

    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    ' ==============================
    ' 2. 添加一个元素
    ' ==============================
    elem.addComponent getTableNameToken
    Debug.Assert elem.toString(config) = "max " & TEST_TABLE_NAME

    ' ==============================
    ' 3. 添加一个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis
    Debug.Assert elem.toString(config) = "max " & TEST_TABLE_NAME & ")"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max " & TEST_TABLE_NAME & ")"

    ' ==============================
    ' 4. 添加多个右括号
    ' ==============================
    elem.addBoundaryRightParenthesis(3)
    Debug.Assert elem.toString(config) = "max " & TEST_TABLE_NAME & "))))"

    Set strArr = elem.toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max " & TEST_TABLE_NAME & "))))"
End Sub

' 测试: getType 方法
Private Sub test_ISqlFormatterElem_getType()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    ' ==============================
    ' 2. 默认为函数类型
    ' ==============================
    Debug.Assert g.ISqlFormatterElem_getType = SqlTokenType.STR_FUNC

End Sub

' 测试: isComment 方法
Private Sub test_ISqlFormatterElem_isComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g
    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isComment = False
End Sub

' 测试: isShareLineComment 方法
Private Sub test_ISqlFormatterElem_isShareLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isShareLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isShareLineComment = False
End Sub

' 测试: isSingleLineComment 方法
Private Sub test_ISqlFormatterElem_isSingleLineComment()
    ' ==============================
    ' 1. 初始化
    ' ==============================
    Dim g As SqlFormatterFuncCol
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")

    Dim elem As ISqlFormatterElem
    Set elem = g

    ' ==============================
    ' 2. 默认为 false
    ' ==============================
    Debug.Assert elem.isShareLineComment = False

    ' ==============================
    ' 3. 即使添加一个注释，也是 false
    ' ==============================
    elem.addComponent getShareLineCommentToken
    Debug.Assert elem.isSingleLineComment = False

    elem.addComponent getSingleLineCommentToken
    Debug.Assert elem.isSingleLineComment = False
End Sub

' 测试: isEndsWithComment 方法
Private Sub test_ISqlFormatterElem_isEndsWithComment()
    ' 参考: test_ISqlFormatterElem_addComponent
End Sub

' 测试: isMultiLine 方法
Private Sub test_ISqlFormatterElem_isMultiLine()
    ' 参考: test_ISqlFormatterElem_addComponent
End Sub

' 测试: toString + toStringArray 方法
Private Sub test_ISqlFormatterElem_toString_toStringArray()
    Dim config As SqlFormatterConfiguration
    Set config = New SqlFormatterConfiguration

    Dim strArr As IPrimitiveArrayRef

    Dim g As SqlFormatterFuncCol

    Dim prefixSpace As String
    prefixSpace = "    "
    ' ==============================
    ' 1. 添加一个单行的函数体
    ' ==============================
    ' 1. 添加一个单行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getSingleLineFuncBody

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max(col1, col2)"

    ' 2. 设置边界括号: 左边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((max(col1, col2)"

    ' 3. 设置边界括号: 左 + 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "((max(col1, col2)))"

    ' 4. 添加一个和前一个元素同一行的注释
    g.ISqlFormatterElem_addComponent getShareLineCommentToken
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 3
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = "    max(col1, col2) --aaaaaa"
    Debug.Assert strArr.Item(2) = "))"

    ' 5. 添加一个单行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getSingleLineFuncBody

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max(col1, col2)"

    '  设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 1
    Debug.Assert strArr.Item(0) = "max(col1, col2)))"


    ' ==============================
    ' 2. 添加一个多行的函数体
    ' ==============================
    ' 1. 添加一个多行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getMultiLineFuncBody

    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 4
    Debug.Assert strArr.Item(0) = "max("
    Debug.Assert strArr.Item(1) = "    col1,"
    Debug.Assert strArr.Item(2) = "    col2"
    Debug.Assert strArr.Item(3) = ")"

    ' 2. 设置边界括号: 左边界括号
    g.ISqlFormatterElem_addBoundaryLeftParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 5
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = prefixSpace & "max("
    Debug.Assert strArr.Item(2) = prefixSpace & "    col1,"
    Debug.Assert strArr.Item(3) = prefixSpace & "    col2"
    Debug.Assert strArr.Item(4) = prefixSpace & ")"

    ' 3. 设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 6
    Debug.Assert strArr.Item(0) = "(("
    Debug.Assert strArr.Item(1) = prefixSpace & "max("
    Debug.Assert strArr.Item(2) = prefixSpace & "    col1,"
    Debug.Assert strArr.Item(3) = prefixSpace & "    col2"
    Debug.Assert strArr.Item(4) = prefixSpace & ")"
    Debug.Assert strArr.Item(5) = "))"

    ' 5. 添加一个多行的函数体
    Set g = New SqlFormatterFuncCol
    g.Init getStrNameToken("max")
    g.ISqlFormatterElem_addComponent getMultiLineFuncBody


    '  设置边界括号: 右边界括号
    g.ISqlFormatterElem_addBoundaryRightParenthesis(2)
    Set strArr = g.ISqlFormatterElem_toStringArray(config)
    Debug.Assert strArr.Count = 5
    Debug.Assert strArr.Item(0) = prefixSpace & "max("
    Debug.Assert strArr.Item(1) = prefixSpace & "    col1,"
    Debug.Assert strArr.Item(2) = prefixSpace & "    col2"
    Debug.Assert strArr.Item(3) = prefixSpace & ")"
    Debug.Assert strArr.Item(4) = "))"
End Sub